package bgate.hung.activity;

import java.util.Vector;

import bgate.connector.DataTransfer;
import bgate.hung.workerclass.StaticData;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;
import android.view.View.OnClickListener;

public class ChoiseGame extends Activity {
	private static final String TAG = "ChoiseGameActivity";
	private DataTransfer dataTransfer = null;
	private IncommingMessage incommingMessage = null;

	private Button phomButton, tienlenButton, bacayButton, naptienButton;
	private String account = "";
	private boolean isPushTask = false, isButtonPressed = false;
	private int game;
	private Vector<Integer> ga_vector = new Vector<Integer>();
	private int[] ga_array = new int[10];
	private ProgressDialog progressDialog;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		Intent intent = getIntent();
		account = intent.getStringExtra("ACCOUNT");

		dataTransfer = DataTransfer.getInstance();
		if (incommingMessage == null)
			incommingMessage = new IncommingMessage(dataTransfer.getOutQueue());
		if (!isPushTask && incommingMessage != null) {
			dataTransfer.putTask(incommingMessage);
			isPushTask = true;
		}

		setContentView(R.layout.choise_game_layout);
		phomButton = (Button) findViewById(R.id.phomButton_choise_game_layout);
		tienlenButton = (Button) findViewById(R.id.tienlenButton_choise_game_layout);
		bacayButton = (Button) findViewById(R.id.bacayButton_choise_game_layout);
		naptienButton = (Button) findViewById(R.id.naptienButton_choise_game_layout);
		
		progressDialog = new ProgressDialog(ChoiseGame.this);
		progressDialog.setMessage("Xin chờ ...");

		phomButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!isButtonPressed) {
					progressDialog.show();
					game = StaticData.GAME_PHOM;
					sendGARequest(game);
					isButtonPressed = true;
				}
			}
		});
	}
	
	public void onResume() {
		super.onResume();
		dataTransfer = DataTransfer.getInstance();
		if (incommingMessage == null)
			incommingMessage = new IncommingMessage(dataTransfer.getOutQueue());
		if (!isPushTask && incommingMessage != null) {
			dataTransfer.putTask(incommingMessage);
			isPushTask = true;
		}
		isButtonPressed = false;
	}
	
	public void onPause() {
		super.onPause();
		if (isPushTask) {
			dataTransfer.removeTask(incommingMessage);
			isPushTask = false;
		}
		incommingMessage = null;
	}

	private void sendGARequest(int i) {
		dataTransfer.requestData("}GA|" + Integer.toString(i) + "&");
	}
	
	private void enterChoiseRoom() {
		for (int i = 0; i < ga_array.length; i++) {
			if (i < ga_vector.size()) {
				ga_array[i] = ga_vector.elementAt(i);
			}
		}
		Intent intent = new Intent(this, ChoiseRoom.class);
		intent.putExtra("GA_ARRAY", ga_array);
		intent.putExtra("ACCOUNT", account);
		progressDialog.cancel();
		startActivity(intent);
	}

	private class IncommingMessage implements Runnable {
		private final Vector mInQueue;

		public IncommingMessage(Vector inQueue) {
			this.mInQueue = inQueue;
		}

		public void run() {

			try {
				Thread.sleep(10);
				if (mInQueue.size() > 0) {

					String data = (String) mInQueue.elementAt(0);
					mInQueue.removeElementAt(0);
					Log.d(TAG, data);
					Vector<String> tempResultData = splitString(data, '}');
					for (int k = 0; k < tempResultData.size(); k++) {
						Vector<String> resultData = splitString(
								tempResultData.elementAt(k), '|');
						for (int i = 0; i < resultData.size(); i++) {
							String s = resultData.elementAt(i);
							if (s.equals("GA")) {
								for(int j = i + 1; j < resultData.size(); j++) {
									s = resultData.elementAt(j);
									ga_vector.addElement(Integer.valueOf(s));
								}
								enterChoiseRoom();
							}
						}
					}
				}
			} catch (InterruptedException ex) {
				Log.d(TAG, ex.toString());
			} catch (NullPointerException ex) {
				Log.d(TAG, ex.toString());
			}

		}

	}

	private Vector<String> splitString(String string, char delimiter) {
		Vector<String> mResult = new Vector<String>();
		TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(
				delimiter);

		// Once per string to split
		splitter.setString(string);
		for (String s : splitter) {
			String temp = "";
			for (int i = 0; i < s.length(); i++) {
				if (s.charAt(i) != '}' && s.charAt(i) != '&')
					temp += s.charAt(i);
			}
			mResult.addElement(temp);
		}

		return mResult;
	}
}
